﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TireService.Data;
using System.Diagnostics;

namespace TireService.Mobile
{
    public partial class Customers : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnSearchCustomer_Click(object sender, EventArgs e)
        {
            TireServiceDB db = new TireServiceDB();
            try
            {
                if (rbSearchFilter.SelectedItem.Value.ToString() == "0")
                {
                    try
                    {
                        int CustomerId = Convert.ToInt32(txtSearchString.Text);
                        gvCustomers.DataSource = db.Customers.Where(p => p.CustomerId == CustomerId).Take(10).ToList();
                    }
                    catch (Exception ex)
                    {
                        // TODO: KundeId ikke gyldigt
                    }
                    
                }
                if (rbSearchFilter.SelectedItem.Value.ToString() == "1")
                {
                    // TODO: Fix nedenstående, ikke korrekt filter
                    //gvCustomers.DataSource = db.Customers.Where(p => p.CustomerId == Convert.ToInt32(txtSearchString.Text)).Take(10).ToList();

                    Car car = db.Cars.SingleOrDefault(p => p.RegistrationNumber == txtSearchString.Text);

                    if (car != null)
                    {
                        gvCustomers.DataSource = db.Customers.Where(p => p.CustomerId == car.CustomerId).Take(10).ToList();
                    }

                }
                if (rbSearchFilter.SelectedItem.Value.ToString() == "2")
                {
                    gvCustomers.DataSource = db.Customers.Where(p => p.Name.Contains(txtSearchString.Text)).Take(10).ToList();
                }


                gvCustomers.DataBind();
            }
            catch (Exception)
            {
                
            }
            
        }
    }
}